package com.peppa.petshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peppa.petshop.entity.*;
import com.peppa.petshop.mapper.*;
import com.peppa.petshop.service.IndentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peppa.petshop.vo.DataVO;
import com.peppa.petshop.vo.GoodVO;
import com.peppa.petshop.vo.IndentItemVO;
import com.peppa.petshop.vo.IndentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
@Service
public class IndentServiceImpl extends ServiceImpl<IndentMapper, Indent> implements IndentService {
    @Autowired(required = false)
    private IndentMapper indentMapper;
    @Autowired(required = false)
    private CustomerMapper customerMapper;
    @Autowired(required = false)
    private AddressMapper addressMapper;
    @Autowired(required = false)
    private IndentItemMapper indentItemMapper;
    @Autowired(required = false)
    private GoodMapper goodMapper;
    @Autowired(required = false)
    private CartMapper cartMapper;

    public DataVO<IndentVO> getIndentVO(Integer page,Integer limit) {
        List<IndentVO> indentVOs = new ArrayList<>();
        if (page== null){
            page=1;
            limit = 10;
        }

        IPage<Indent> indentIPage = new Page<>(page,limit);
        IPage<Indent> result = indentMapper.selectPage(indentIPage, null);
        List<Indent> indents = result.getRecords();
        Integer count = (int) result.getTotal();
        for (Indent indent : indents) {
            IndentVO indentVO = new IndentVO();
            BeanUtils.copyProperties(indent,indentVO);
            indentVO.setCustomer(customerMapper.selectById(indent.getCustomer()).getCustomerName());
            indentVO.setAddress(addressMapper.selectById(indent.getAddress()).getAddressName());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("indent",indent.getIndentId());
            List<IndentItem> items = indentItemMapper.selectList(queryWrapper);
            ArrayList<IndentItemVO> itemVOs = new ArrayList<>();
            for (IndentItem item : items) {
                IndentItemVO itemVO = new IndentItemVO();
                BeanUtils.copyProperties(item,itemVO);
                Good good = goodMapper.selectById(item.getGood());
                BeanUtils.copyProperties(good,itemVO);
                itemVO.setGood(good.getGoodName());
                itemVO.setGoodId(good.getGoodId());
                itemVO.setUnivalence(good.getPrice());
                itemVOs.add(itemVO);
            }
            indentVO.setIndentItems(itemVOs);
//            indentVO.setIndentItems(indentItemMapper.selectList(queryWrapper));
            indentVOs.add(indentVO);
        }
        DataVO<IndentVO> indentVOList = new DataVO<>(new Integer(0),"",count,indentVOs);
        return indentVOList;
    }

    @Override
    public DataVO<IndentVO> getIndentVOById(Integer page, Integer limit, Integer indentId) {
        List<IndentVO> indentVOs = new ArrayList<>();
        if (page== null){
            page=1;
            limit = 10;
        }
        IPage<Indent> indentIPage = new Page<>(page,limit);
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("indent_id",indentId);
        IPage<Indent> result = indentMapper.selectPage(indentIPage, queryWrapper1);
        List<Indent> indents = result.getRecords();
        Integer count = (int) result.getTotal();
        for (Indent indent : indents) {
            IndentVO indentVO = new IndentVO();
            BeanUtils.copyProperties(indent,indentVO);
            indentVO.setCustomer(customerMapper.selectById(indent.getCustomer()).getCustomerName());
            indentVO.setAddress(addressMapper.selectById(indent.getAddress()).getAddressName());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("indent",indent.getIndentId());
            List<IndentItem> items = indentItemMapper.selectList(queryWrapper);
            ArrayList<IndentItemVO> itemVOs = new ArrayList<>();
            for (IndentItem item : items) {
                IndentItemVO itemVO = new IndentItemVO();
                BeanUtils.copyProperties(item,itemVO);
                Good good = goodMapper.selectById(item.getGood());
                BeanUtils.copyProperties(good,itemVO);
                itemVO.setGood(good.getGoodName());
                itemVO.setGoodId(good.getGoodId());
                itemVO.setUnivalence(good.getPrice());
                itemVOs.add(itemVO);
            }
            indentVO.setIndentItems(itemVOs);
//            indentVO.setIndentItems(indentItemMapper.selectList(queryWrapper));
            indentVOs.add(indentVO);
        }
        DataVO<IndentVO> indentVOList = new DataVO<>(new Integer(0),"",count,indentVOs);
        return indentVOList;
    }

    @Override
    public DataVO<IndentVO> getIndentVOByStatus(Integer page, Integer limit, Integer indentStatus) {
        List<IndentVO> indentVOs = new ArrayList<>();
        if (page== null){
            page=1;
            limit = 10;
        }
        IPage<Indent> indentIPage = new Page<>(page,limit);
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("indent_status",indentStatus);
        IPage<Indent> result = indentMapper.selectPage(indentIPage, queryWrapper1);
        List<Indent> indents = result.getRecords();
        Integer count = (int) result.getTotal();
        for (Indent indent : indents) {
            IndentVO indentVO = new IndentVO();
            BeanUtils.copyProperties(indent,indentVO);
            indentVO.setCustomer(customerMapper.selectById(indent.getCustomer()).getCustomerName());
            indentVO.setAddress(addressMapper.selectById(indent.getAddress()).getAddressName());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("indent",indent.getIndentId());
            List<IndentItem> items = indentItemMapper.selectList(queryWrapper);
            ArrayList<IndentItemVO> itemVOs = new ArrayList<>();
            for (IndentItem item : items) {
                IndentItemVO itemVO = new IndentItemVO();
                BeanUtils.copyProperties(item,itemVO);
                Good good = goodMapper.selectById(item.getGood());
                BeanUtils.copyProperties(good,itemVO);
                itemVO.setGood(good.getGoodName());
                itemVO.setGoodId(good.getGoodId());
                itemVO.setUnivalence(good.getPrice());
                itemVOs.add(itemVO);
            }
            indentVO.setIndentItems(itemVOs);
//            indentVO.setIndentItems(indentItemMapper.selectList(queryWrapper));
            indentVOs.add(indentVO);
        }
        DataVO<IndentVO> indentVOList = new DataVO<>(new Integer(0),"",count,indentVOs);
        return indentVOList;
    }

    @Override
    @Transactional
    public String insert(Integer customerId, List<Integer> goodIds, Integer address) {

        BigDecimal totalPrice = new BigDecimal("0");
        for (Integer id : goodIds) {
            Good good = goodMapper.selectById(id);
            Integer count = good.getCount();
            if (count<=0){
                goodIds.remove(id);
            }else{
                good.setCount(good.getCount()-1);
                goodMapper.updateById(good);
                QueryWrapper queryWrapper =new QueryWrapper();
                queryWrapper.eq("customer",customerId);
                queryWrapper.eq("good",good.getGoodId());
                cartMapper.delete(queryWrapper);
                totalPrice = totalPrice.add(good.getPrice());
            }
        }
        Integer id = null;
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        Indent indent = new Indent(id,customerId,address,totalPrice, now,1, now,0);
        System.out.println(indent);
//        int insert = indentMapper.insert(indent);

        indentMapper.insertReturnId(indent);
        Integer indentId = indent.getIndentId();
        for (Integer integer : goodIds) {
            IndentItem indentItem = new IndentItem();
            indentItem.setAmount(1);
            indentItem.setCreateTime(now);
            indentItem.setGood(integer);
            indentItem.setPayTime(now);
            indentItem.setIndent(indentId);
            Good good = goodMapper.selectById(integer);
            indentItem.setTotalPrice(good.getPrice());
            indentItem.setUnivalence(good.getPrice());
            indentItemMapper.insert(indentItem);
        }
        return "true";
    }

    @Override
    public String sureIndent(Integer indentId) {
        Indent indent = indentMapper.selectById(indentId);
        indent.setIndentStatus(2);
        int i = indentMapper.updateById(indent);
        if (i==1){
            return "订单状态修改成功！";
        }
        return "修改失败";

    }

    @Override
    @Transactional
    public String returnPet(Integer indentId) {
        Indent indent = indentMapper.selectById(indentId);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("indent",indent);
        List<IndentItem> list = indentItemMapper.selectList(wrapper);
        for (IndentItem item : list) {
            Good good = goodMapper.selectById(item.getGood());
            good.setCount(good.getCount()+item.getAmount());
            goodMapper.updateById(good);
            indentItemMapper.deleteById(item.getIndentItemId());
        }
        indentMapper.deleteById(indentId);
        return "退货退款成功！";
    }

}
